<template>
  <div class="case-reg-page">
    <d2-crud
      ref="d2Crud"
      index-row
      selection-row
      :columns="columns"
      :data="dataList"
      :options="options"
      :rowHandle="rowHandle"
      :pagination="pagination"
      @edit-model="mod"
      @reg-model="register"
      @sort-change="sortChange"
      @selection-change="selectChange"
      @row-click="rowClick"
      @pagination-current-change="pageChange"

    >
      <div class="title" slot="header">
        <span class="text">案件登记</span>
        <list-search :cols="columns" @search=""/>
          <head-btns @load="pageBtns" @add="add" @reg="register"/>
      </div>
    </d2-crud>
  </div>
</template>
<script>
import detailInfo from "./detail-info";
import discretionApi from "@/api/system/discretion";
import caseApi from "@/api/system/case";
import utils from "@/assets/utils/zhzf-util";
import dictCache from "@/assets/utils/cache-dict-util";
import headBtns from "@/views/comps/zhzf/head-btns";
import listSearch from "@/views/comps/zhzf/list-search";
import { mapState, mapMutations } from "vuex";
import { frameInRoutes } from "@/router/routes";
import layoutHeaderAside from "@/views/layout/header-aside";
import tabPageMinxin from "@/assets/minxin/tag-page";
import detailView from "@/views/case/reg/detail-info";
export default {
  components: { headBtns, detailInfo,listSearch  },
  mixins: [tabPageMinxin],
  data() {
    return {
      dataList: [],
      rowHandle: {},
      columns: [
        { title: "案件编号", key: "caseNum", width: 180, sortable: "true" },
        { title: "立案地点", key: "fillingPlace", sortable: "true" },
        {
          title: "立案时间",
          key: "createTime",
          width: 150,
          sortable: "true",
          formatter: (row, col, val) => {
            return utils.formatDts(val, "YYYY-MM-DD HH:mm:ss");
          }
        },
        { title: "立案机构", key: "organName", width: 200, sortable: "true" },
        { title: "当事人", key: "party", width: 80, sortable: "true" },
        {
          title: "执行类型",
          key: "caseTypeId",
          width: 80,
          sortable: "true",
          formatter: (row, col, val) => {
            let dict = dictCache.getDataInfo("0048", val);
            return (dict && dict.name) || "一般程序";
          }
        },
        {
          title: "执法门类",
          key: "zfml",
          width: 80,
          sortable: "true",
        },
        { title: "执法人", key: "staffstr", width: 120, sortable: "true",
          formatter: (row, col, val) => {
            let dict = row;
            if(dict.statffs.length>0){
              return (dict.statffs[0].username)
            }else{
              return '--'
            }
          }
        },
        { title: "案件类型", key: "caseType", width: 80, sortable: "true" },
      ],
      options: {
        stripe: true,
        highlightCurrentRow: true
      },
      pagination: {
        currentPage: 1,
        pageSize: 15,
        total: 0
      },
      queryParam: {},
      selected: [],
      isSignal:false
    };
  },
  computed: {
    ...mapState("d2admin/user", ["info"])
  },
  mounted() {
    this.initPage();
  },
  methods: {
    ...mapMutations({
      pageInit: "d2admin/page/init",
      headerSet: "d2admin/menu/headerSet"
    }),

    pageBtns(btns) {
      btns = btns || [];
      this.rowHandle = {
        width: 80,
        custom: []
      };
      btns.forEach(item => {
          this.rowHandle.custom.push({
            text: item.name,
            size: "mini",
            emit: `${item.url}-model`,
            icon: item.icon,
            style: `color:${item.url == "del" ? "#f53c1b" : "#1B98F5"}`
          });
      });
      this.rowHandle.custom.splice(0,1);
      this.rowHandle.custom.push({
            text: "详情",
            size: "mini",
            emit: "edit-model",
            style: "color:#409eff;text-decoration:underline;font-size:14px;"
          })
    },

    async sortChange(column, prop, order) {
      //根据表头参数进行排序
      await this._fetchListData();
    },
    rowClick(row) {
      let els = this.$refs.d2Crud.$children;
      this.$refs.d2Crud.$children[els.length == 1 ? 0 : els.length - 2].toggleRowSelection(row);
    },
    selectChange(selection) {
      this.selected = selection;
    },
    register(rd) {
      if (rd && rd.row) {
        this.registers(rd.row);
      } else if (this.selected.length == 1) {
        this.registers(this.selected[0]);
      } else {
        utils.showTips('请选择一行数据', false)
        return;
      }
    },
    async registers(row ) {
      //立案事件
      this.$confirm("确认立案？", "确认信息", {
        distinguishCancelAndClose: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(async () => {
          await this.caseRegister(row);
          await this._fetchListData();
        })
        .catch(() => {});
    },
    async casetypechange() {
      await this._fetchListData();
    },
    async caseRegister(row) {
      let resp = await caseApi.caseHanding({
        id: row.caseAccessId,
        vars: { caseCreateUser: this.info.username},
        // processDefineKey:'general_road_transport_handle'
      }); //启动流程
      if (resp) {
        let ok = resp.ok;
        let taskId = resp.data.taskId
        if (ok) {
            let res = await caseApi.caseExamine({//立案流程
              taskId: taskId,
              commentMessage: "11111",
              commentType: "",
              vars: {}
            });
            if (res.ok) {
              utils.showTips(res.ok ? "立案成功" : "立案失败", res.ok);
            }
        }
      }
    },
    async initPage() {
      await this._fetchListData();
    },
    async _fetchListData() {
      let param = {};
      param.caseProcessStarted = "false";
      let res = await caseApi.pageList({ ...this.pagination, ...param });
      this.dataList = res.data.rows;
      if (res.status != 0) {
        utils.showTips("案件添加失败！");
      }
      this.pagination.total = res.data.total || this.dataList.length;
    },
    pageChange(currentPage) {
      this.pagination.currentPage = currentPage;
      this._fetchListData();
    },
    showDetail(row) {
      let pageTitle = "案件登记-录入";
      let rpath = "case/reg/detail";
      let rname = "caseRegDetail";
      this.addTabPage(pageTitle, rpath, rname, detailView, { row: row });
    },
    add() {
      //登记跳路由
      this.showDetail();
    },
    mod(rd) {
      if (rd&&rd.row) {
        this.showDetail(rd.row);
      } else if (this.selected.length == 1) {
        this.showDetail(this.selected[0]);
      } else {
        utils.showTips("请选择一行数据");
        return;
      }
    },
    async submitDel(ids) {
      await discretionApi.del(ids);
      await this.initPage();
    }
  }
};
</script>
<style lang="scss">
.case-reg-page {
  .d2-crud-body {
  }
}
</style>
